<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action {
    public function index(){
        //header("Content-Type:text/html; charset=utf-8");
        /**
		  * wechat php test
		  */
		 
		//import("@.Util.wechatCallbackapiTest");
		//define your token
		define("TOKEN", "zhen51L");
		//$wechatObj = new wechatCallbackapiTest();
		$this->valid();

    }
	
	public function test(){
		$keyword = 1;
		$bgPicture = M('background');
		$con['aid'] = $keyword;
		$bgPicList = $bgPicture->where($con)->select();
		$picUrl = C('MYSITE').APP_TMPL_PATH.'/Public/Images/bg/'.$bgPicList[0]['imgurl'];
		
		$url = C('MYSITE').U("Words/showWordsById",array('aid'=>$keyword));
		
		echo 'pu: '.$picUrl.'<br />'.'url: '.$url;
	}
	
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	//echo $echoStr;
        	$this->responseMsg();
			//echo "hello";
			exit;
        }
    }

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];		

      	//extract post data
		if (!empty($postStr)){
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$fromUsername = $postObj->FromUserName;
			$toUsername = $postObj->ToUserName;
			$time = time();
			$msgType = $postObj->MsgType;
			$keyword = trim($postObj->Content);

			if($msgType == "text"){
				$msgType = "news";
				$textTpl = "<xml>
					   <ToUserName><![CDATA[%s]]></ToUserName>
					   <FromUserName><![CDATA[%s]]></FromUserName>
					   <CreateTime>%s</CreateTime>
					   <MsgType><![CDATA[%s]]></MsgType>
					   <Content><![CDATA[]]></Content>
					   <ArticleCount>1</ArticleCount>
					   <Articles>
					   <item>
					   <Title><![CDATA[%s]]></Title>
					   <Description><![CDATA[%s]]></Description>
					   <PicUrl><![CDATA[%s]]></PicUrl>
					   <Url><![CDATA[%s]]></Url>
					   </item>
					   </Articles>
					   <FuncFlag>0</FuncFlag>
					   </xml>";
				//$keyword = intval($keyword,10);
				
				if(1<= $keyword && $keyword <=100){
					//根据关键字查找作者信息$title,$description,$picUrl,$url
					$author = M('author');
					$chosenAuthor = $author->find($keyword);
					$title = $chosenAuthor['chname'];
					$description = $chosenAuthor['desc'];
					
					$bgPicture = M('background');
					$con['aid'] = $keyword;
					$bgPicList = $bgPicture->where($con)->select();
					$picUrl = C('MYSITE').APP_TMPL_PATH.'/Public/Images/bg/'.$bgPicList[0]['imgurl'];
					
					$url = C('MYSITE').U("Words/showWordsById",array('aid'=>$keyword));
					
					$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$title,$description,$picUrl,$url);
				}else{
					$msgType = "text";
					$keyword = "输入不正确";
					$textTpl = "<xml>
								<ToUserName><![CDATA[%s]]></ToUserName>
								<FromUserName><![CDATA[%s]]></FromUserName>
								<CreateTime>%s</CreateTime>
								<MsgType><![CDATA[%s]]></MsgType>
								<Content><![CDATA[%s]]></Content>
								<FuncFlag>0<FuncFlag>
								</xml>";
					$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$keyword);
				}
				echo $resultStr;
			  }
			}else {
			   echo "请输入1-100";
			   exit;
			}
    }
		
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}